<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                  |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                     |
// +------------------------------------------------------------------------+
// | Author: shaoshuai@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//
namespace AuctionBack\Controller;

use AuctionCore\Common\AjaxPage;
use AuctionCore\Common\Image;
use AuctionCore\Common\Util;

class QuestionController extends BaseController
{
    private $questionLogic;

    private $questionImagesLogic;

    public function __construct()
    {
        parent::__construct();

        $this->questionLogic = D('Question', 'Logic');

        $this->questionImagesLogic = D('QuestionImages', 'Logic');

    }

    public function imgAudit()
    {
        $this->display();
    }

    /**
     * 获取图片列表
     */
    public function getPicList()
    {
        $page = I('get.p', 1);
        $pageSize = 18;
        $status = I('get.status');

        $data = $this->questionImagesLogic->getAuditImages($status, $page, $pageSize);

        foreach ($data['list'] as &$v) {
            $v['url'] = Image::get_full_url($v['url']);
        }

        $Page = new AjaxPage($data['total'], $pageSize, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();// 分页显示输出

        $this->assign('page', $show);
        $this->assign('img', $data['list']);

        $tpl = $status == 1 ? '/Question/getPassedList' : '';

        $this->display($tpl);
    }

    /**
     * 图片审核通过
     */
    public function audit()
    {
        $pass = I('post.pass');
        $illegal = I('post.illegal');
        $time = time();

        $model = M('question', 'clcw_', 'DB_CLCW');
        $model->startTrans();

        //如果有审核通过的图片
        if ($pass) {
            //通过图片问题ID
            $passImgQuestionId = $this->questionImagesLogic->getImgQuestionId($pass);
            //通过问题
            $res = $this->questionLogic->passQuestion($passImgQuestionId);
            if (!$res) {
                $model->rollback();
                echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '5', 'msg' => '2']);
                die;
            }

            //图片审核通过
            $res = $this->questionImagesLogic->passImage($pass);
            if (!$res) {
                $model->rollback();
                echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '5', 'msg' => '']);
                die;
            }

            //发送钉钉消息
            foreach ($passImgQuestionId as $v) {
                $questionInfo = $this->questionLogic->getQuestion($v);
                $userInfo = D('ClcwUser', 'Logic')->getUserInfoByUid($questionInfo['user_id']);
                $userNickName = $userInfo['nick_name'];

                if (empty($userInfo['nick_name'])) {
                    $userNickName = showHideMobile($userInfo['mobile']);
                }
                $dingDingMsgContent = "【开新用车】用户“" . $userNickName . "”提问了，问题是“" . $questionInfo['content'] . "”";
                if ($questionInfo['expert_id'] > 0) {
                    $expertInfo = D('ClcwUser', 'Logic')->getUserInfoByUid($questionInfo['expert_id']);
                    $expertNickName = $expertInfo['nick_name'];
                    if (empty($expertInfo['nick_name'])) {
                        $expertNickName = showHideMobile($expertInfo['mobile']);
                    }
                    $dingDingMsgContent = "【开新用车】用户“" . $userNickName . "”向牛人“" . $expertNickName . "”付费提问了，问题是“" . $questionInfo['content'] . "”";
                }
                sendDingDingMsg($dingDingMsgContent);
            }
        }
        //如果有违规图片
        if ($illegal) {
            //查询违规图片的问题ID
            $illegalQuestionID = $this->questionImagesLogic->getIllegalImgQuestionID($illegal);
            //违规问题禁用
            $res = $this->questionLogic->delIllegaQuestion($illegalQuestionID);
            if (!$res) {
                $model->rollback();
                echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '5', 'msg' => '2']);
                die;
            }
            //删除图片
            $res = $this->questionImagesLogic->delIllegalImg($illegal);
            if (!$res) {
                $model->rollback();
                echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '5', 'msg' => '2']);
                die;
            }
        }

        $model->commit();
        echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '200', 'msg' => '']);
        die;
    }

    /**
     * 删除图片
     */
    public function delPic()
    {
        $illegal = I('post.illegal');

        $model = M('question', 'clcw_', 'DB_CLCW');
        $model->startTrans();

        //如果有违规图片
        if ($illegal) {
            //查询违规图片的问题ID
            $illegalQuestionID = $this->questionImagesLogic->getIllegalImgQuestionID($illegal);
            //违规问题禁用
            $res = $this->questionLogic->delIllegaQuestion($illegalQuestionID);

            if (!$res) {
                $model->rollback();
                echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '5', 'msg' => '2']);
                die;
            }
            //删除图片
            $res = $this->questionImagesLogic->delIllegalImg($illegal);
            if (!$res) {
                $model->rollback();
                echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '5', 'msg' => '2']);
                die;
            }
        }

        $model->commit();
        echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '200', 'msg' => '']);
        die;
    }

    /**
     * 问题类型管理
     */
    public function questionCategory()
    {
        $list = D('Question', 'Logic')->getQuestionCategoryList();
        $this->assign('data', $list);
        $this->display();
    }

    /**
     * 添加提问类型
     */
    public function addCategory()
    {
        $id = I('request.id');
        $category = D('Question', 'Logic')->getQuestionCategory($id);
        $this->assign('category', $category);
        $this->display();
    }

    /**
     * 保存提问类型
     */
    public function saveQuestionCategory()
    {
        $data = [];
        $data['id'] = I('request.id', 0);
        $data['category_name'] = I('request.category_name', '');
        if (isset($_REQUEST['status'])) {
            $data['status'] = I('request.status', 0);
        }

        $res = D('Question', 'Logic')->saveQuestionCategory($data);
        if ($res) {
            $this->helpJsonResult('200', '保存成功');
        } else {
            $this->helpJsonResult('300', '保存失败');
        }
    }
}
